const Mock = require('mockjs')

// 生成随机数据
function generateRandomData() {
  const userStats = {
    total: Mock.Random.integer(100000, 200000),
    growth: Mock.Random.float(5, 25, 1, 1),
    newUsers: Mock.Random.integer(200, 800),
    activeUsers: Mock.Random.integer(5000, 15000)
  }

  const shopOrderStats = {
    total: Mock.Random.integer(2000000, 5000000),
    growth: Mock.Random.float(3, 20, 1, 1),
    orderCount: Mock.Random.integer(1000, 5000),
    avgOrderValue: Mock.Random.integer(200, 800)
  }

  const rechargeOrderStats = {
    total: Mock.Random.integer(1500000, 4000000),
    growth: Mock.Random.float(5, 30, 1, 1),
    orderCount: Mock.Random.integer(500, 3000),
    avgOrderValue: Mock.Random.integer(100, 500)
  }

  return { userStats, shopOrderStats, rechargeOrderStats }
}

// 生成趋势数据
function generateTrendData(days) {
  const dates = []
  const userValues = []
  const shopValues = []
  const rechargeValues = []
  
  const today = new Date()
  
  for (let i = days - 1; i >= 0; i--) {
    const date = new Date(today)
    date.setDate(date.getDate() - i)
    dates.push(date.toISOString().split('T')[0])
    
    // 生成带有趋势的随机数据
    const baseUser = 200 + (days - i) * 2
    const baseShop = 20000 + (days - i) * 100
    const baseRecharge = 15000 + (days - i) * 80
    
    userValues.push(baseUser + Mock.Random.integer(-50, 100))
    shopValues.push(baseShop + Mock.Random.integer(-5000, 10000))
    rechargeValues.push(baseRecharge + Mock.Random.integer(-3000, 8000))
  }
  
  return { dates, userValues, shopValues, rechargeValues }
}

// 生成表格数据
function generateTableData(startDate, endDate) {
  const data = []
  const start = new Date(startDate)
  const end = new Date(endDate)
  const days = Math.ceil((end - start) / (1000 * 60 * 60 * 24)) + 1
  
  for (let i = 0; i < days; i++) {
    const date = new Date(start)
    date.setDate(date.getDate() + i)
    
    const newUsers = Mock.Random.integer(50, 300)
    const shopOrders = Mock.Random.integer(10, 100)
    const rechargeOrders = Mock.Random.integer(5, 80)
    const shopAmount = Mock.Random.integer(10000, 100000)
    const rechargeAmount = Mock.Random.integer(5000, 80000)
    
    data.push({
      date: date.toISOString().split('T')[0],
      newUsers,
      totalUsers: 125680 + Mock.Random.integer(0, 1000),
      shopOrders,
      shopAmount,
      rechargeOrders,
      rechargeAmount,
      totalAmount: shopAmount + rechargeAmount
    })
  }
  
  return data
}

module.exports = [
  // 获取用户统计数据
  {
    url: '/api/dashboard/user-stats',
    type: 'get',
    response: () => {
      const { userStats } = generateRandomData()
      return {
        code: 200,
        message: 'success',
        data: userStats
      }
    }
  },

  // 获取商城订单统计数据
  {
    url: '/api/dashboard/shop-order-stats',
    type: 'get',
    response: () => {
      const { shopOrderStats } = generateRandomData()
      return {
        code: 200,
        message: 'success',
        data: shopOrderStats
      }
    }
  },

  // 获取充值订单统计数据
  {
    url: '/api/dashboard/recharge-order-stats',
    type: 'get',
    response: () => {
      const { rechargeOrderStats } = generateRandomData()
      return {
        code: 200,
        message: 'success',
        data: rechargeOrderStats
      }
    }
  },

  // 获取用户增长趋势数据
  {
    url: '/api/dashboard/user-trend',
    type: 'get',
    response: (config) => {
      const { days = 30 } = config.query
      const trendData = generateTrendData(parseInt(days))
      return {
        code: 200,
        message: 'success',
        data: {
          dates: trendData.dates,
          values: trendData.userValues
        }
      }
    }
  },

  // 获取订单金额对比数据
  {
    url: '/api/dashboard/order-comparison',
    type: 'get',
    response: (config) => {
      const { days = 30 } = config.query
      const trendData = generateTrendData(parseInt(days))
      return {
        code: 200,
        message: 'success',
        data: {
          dates: trendData.dates,
          shopValues: trendData.shopValues,
          rechargeValues: trendData.rechargeValues
        }
      }
    }
  },

  // 获取详细数据表格
  {
    url: '/api/dashboard/detail-table',
    type: 'get',
    response: (config) => {
      const { startDate, endDate } = config.query
      const tableData = generateTableData(
        startDate || new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString().split('T')[0],
        endDate || new Date().toISOString().split('T')[0]
      )
      return {
        code: 200,
        message: 'success',
        data: tableData
      }
    }
  },

  // 获取实时数据
  {
    url: '/api/dashboard/real-time',
    type: 'get',
    response: () => {
      const { userStats, shopOrderStats, rechargeOrderStats } = generateRandomData()
      return {
        code: 200,
        message: 'success',
        data: {
          userStats,
          shopOrderStats,
          rechargeOrderStats,
          timestamp: new Date().toISOString()
        }
      }
    }
  }
]
